package com.visikard.request;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.visikard.application.Constant;
import com.visikard.item.CouponBuilderItem;
import com.visikard.item.ItemCouponsBuilder;
import com.visikard.item.ItemPlaceGoogle;
import com.visikard.item.ItemRedeemDetails;
import com.visikard.item.ItemTransactionHistory;
import com.visikard.item.ItemTransactionRewardPunchKard;

public class ParseRequesUtils {

	public static boolean isValidJsonArray(String jarray) {
		return jarray.startsWith("[") && jarray.endsWith("]");
	}

	public static boolean isValidJsonObject(String jarray) {
		return jarray.startsWith("{") && jarray.endsWith("}");
	}

	public static ArrayList<ItemRedeemDetails> parseTransactionHistoryDetails(String jsonArray) {
		ArrayList<ItemRedeemDetails> itemTransaction = new ArrayList<ItemRedeemDetails>();

		if (!jsonArray.isEmpty() && isValidJsonArray(jsonArray)) {
			try {
				JSONArray jarray = new JSONArray(jsonArray);
				for (int i = 0; i < jarray.length(); i++) {
					ItemRedeemDetails item = new ItemRedeemDetails(jarray.getJSONObject(i));
					if (!itemTransaction.contains(item)) {
						itemTransaction.add(item);
					}
				}
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}

		return itemTransaction;
	}

	public static ArrayList<ItemTransactionHistory> parseTransactionHistoryNew(JSONArray jsonArray) {
		ArrayList<ItemTransactionHistory> itemTransaction = new ArrayList<ItemTransactionHistory>();

		try {
			for (int i = 0; i < jsonArray.length(); i++) {
				ItemTransactionHistory item = new ItemTransactionHistory(jsonArray.getJSONObject(i));
				if (!itemTransaction.contains(item)) {
					itemTransaction.add(item);
				}
			}
		}
		catch (Exception e) {
			Log.e(Constant.LOG_TAG, "parseItemGiftKardList " + e.getMessage());
		}

		return itemTransaction;
	}

	public static ArrayList<ItemTransactionRewardPunchKard> parseTransactionRewardPunchKard(String jsonArray) {
		ArrayList<ItemTransactionRewardPunchKard> itemTransaction = new ArrayList<ItemTransactionRewardPunchKard>();

		if (!jsonArray.isEmpty() && isValidJsonArray(jsonArray)) {
			try {
				JSONArray jarray = new JSONArray(jsonArray);
				for (int i = 0; i < jarray.length(); i++) {
					ItemTransactionRewardPunchKard item = new ItemTransactionRewardPunchKard(jarray.getJSONObject(i));
					itemTransaction.add(item);
				}
			}
			catch (Exception e) {
				Log.e(Constant.LOG_TAG, "parseTransactionRewardPunchKard " + e.getMessage());
			}
		}

		return itemTransaction;
	}

	public static ArrayList<ItemCouponsBuilder> parserListCouponsBuilder(String json) {
		ArrayList<ItemCouponsBuilder> mListItem = new ArrayList<ItemCouponsBuilder>();
		JSONObject jsObj;
		JSONArray jsArray;
		try {
			jsObj = new JSONObject(json);
			if (jsObj.getString("statusCode").equals("200")) {
				String rsult = jsObj.getString("result");
				jsArray = new JSONArray(rsult);
				for (int i = 0; i < jsArray.length(); i++) {
					ItemCouponsBuilder item = new ItemCouponsBuilder(jsArray.getJSONObject(i));
					mListItem.add(item);
				}
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return mListItem;
	}

	public static ArrayList<CouponBuilderItem> parserListCouponsBuilder_(String json) {
		ArrayList<CouponBuilderItem> mListItem = new ArrayList<CouponBuilderItem>();
		JSONObject jsObj;
		JSONArray jsArray;
		try {
			jsObj = new JSONObject(json);
			if (jsObj.getString("statusCode").equals("200")) {
				String rsult = jsObj.getString("result");
				jsArray = new JSONArray(rsult);
				for (int i = 0; i < jsArray.length(); i++) {
					CouponBuilderItem item = new CouponBuilderItem(jsArray.getJSONObject(i));
					mListItem.add(item);
				}
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return mListItem;
	}

	public static ItemPlaceGoogle parseGooglePlaceAPI(JSONObject resultJSON) {
		StringBuilder result = new StringBuilder();
		String lat = "", lon = "";
		ItemPlaceGoogle itemCity = new ItemPlaceGoogle();
		try {
			String locality = "", adminLevel1 = "", adminLevel2 = "", country = "";

			JSONArray jArrAaddress_components = resultJSON.getJSONArray("address_components");
			for (int i = 0; i < jArrAaddress_components.length(); i++) {
				JSONObject jsonObject = jArrAaddress_components.getJSONObject(i);
				String shortName = jsonObject.optString("short_name");
				String longName = jsonObject.optString("long_name");
				String type_value = jsonObject.optString("types");
				// Valid item
				if (type_value.contains("postal_code")) {
					itemCity.setZipCode(shortName);
				}
				else if (type_value.contains("\"locality\"") || type_value.contains("\"sublocality\"")) {
					locality = shortName;
					itemCity.setCountryName(longName);
				}
				else if (type_value.contains("\"administrative_area_level_2\"")) {
					adminLevel2 = shortName;
					itemCity.setCountryName(longName);
				}
				else if (type_value.contains("\"administrative_area_level_1\"")) {
					adminLevel1 = shortName;
					itemCity.setAdminCode1(shortName);
					itemCity.setAdminName1(longName);
					itemCity.setCountryName(longName);
				}
				else if (type_value.contains("\"country\"")) {
					itemCity.setCountryCode(shortName);
					itemCity.setCountryName(longName);
				}
			}

			JSONObject jLoc = resultJSON.getJSONObject("geometry").getJSONObject("location");
			lat = jLoc.optString("lat");
			lon = jLoc.optString("lng");
			itemCity.setLocation(lat, lon);

			result = result.append(locality);
			if (adminLevel1.equals("")) {
				if (adminLevel2.equals("")) {
					if (!country.equals("")) {
						if (result.length() > 0)
							result.append(", " + country);
						else
							result.append(country);
					}
				}
				else {
					if (result.length() > 0)
						result.append(", " + adminLevel2);
					else
						result.append(adminLevel2);
				}
			}
			else {
				if (result.length() > 0)
					result.append(", " + adminLevel1);
				else
					result.append(adminLevel1);
			}

			itemCity.setFormattedAddress(resultJSON.optString(("formatted_address")));
		}
		catch (Exception e) {

		}

		itemCity.setName(result.toString());
		return itemCity;
	}
}
